//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

#pragma once

#include "DXSample.h"
#include "imgui.h"
#include "dxcapi.h"

using namespace DirectX;

// Note that while ComPtr is used to manage the lifetime of resources on the CPU,
// it has no understanding of the lifetime of resources on the GPU. Apps must account
// for the GPU lifetime of resources to avoid destroying objects that may still be
// referenced by the GPU.
// An example of this can be found in the class method: OnDestroy().
using Microsoft::WRL::ComPtr;

class D3D12HelloTexture : public DXSample
{
public:
    D3D12HelloTexture(UINT width, UINT height, std::wstring name);

    virtual void OnInit();
    virtual void OnUpdate();
    virtual void OnRender();
    virtual void OnDestroy();

private:
    static const UINT FrameCount = 2;
    static const UINT TextureWidth = 1024;
    static const UINT TextureHeight = 1024;
    static const UINT TexturePixelSize = 4;    // The number of bytes used to represent a pixel in the texture.

    struct Vertex
    {
        XMFLOAT3 position;
        XMFLOAT2 uv;
    };

    // Pipeline objects.
    CD3DX12_VIEWPORT m_viewport;
    CD3DX12_RECT m_scissorRect;
    ComPtr<IDXGISwapChain3> m_swapChain;
    ComPtr<ID3D12Device> m_device;
    ComPtr<ID3D12Resource> m_renderTargets[FrameCount];
    ComPtr<ID3D12CommandAllocator> m_commandAllocator;
    ComPtr<ID3D12CommandQueue> m_commandQueue;
    ComPtr<ID3D12RootSignature> m_rootSignature;
    ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
    ComPtr<ID3D12DescriptorHeap> m_srvHeap;
    ComPtr<ID3D12DescriptorHeap> m_feedbackHeap;
    ComPtr<ID3D12DescriptorHeap> m_imguiSrvHeap;
    ComPtr<ID3D12PipelineState> m_pipelineState;
    ComPtr<ID3D12PipelineState> m_pipelineStateFB;
    ComPtr<ID3D12GraphicsCommandList> m_commandList;
    UINT m_rtvDescriptorSize;
    UINT m_srvDescriptorSize;

    // App resources.
    ComPtr<ID3D12Resource> m_vertexBuffer;
    D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
    ComPtr<ID3D12Resource> m_texture;

    // feedback resource
    ComPtr<ID3D12RootSignature> m_rootSignatureFB;
    ComPtr<ID3D12Resource2> m_feedbackResource;
    ComPtr<ID3D12Resource> m_resolvedResource;
    ComPtr<ID3D12Resource> m_resolvedReadback;

    struct SamplerFeedbackConfig {
        DXGI_FORMAT Format;
        UINT16 MipLevels;
    };

    SamplerFeedbackConfig configs[2] = {
        {DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE, 1},
        {DXGI_FORMAT_SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE, 11}
    };

    ComPtr<IDxcBlob> computeShader;

    const UINT m_numTilesWidth = 8;
    const UINT m_numTilesHeight = 8;

    std::vector<D3D12_RESOURCE_BARRIER> m_barrierUavToResolveSrc;
    std::vector<D3D12_RESOURCE_BARRIER> m_barrierResolveSrcToUav;

    // Synchronization objects.
    UINT m_frameIndex;
    HANDLE m_fenceEvent;
    ComPtr<ID3D12Fence> m_fence;
    UINT64 m_fenceValue;

    // Our state
    bool show_demo_window = false;
    bool show_another_window = false;
    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
    std::string m_feedbackText;

    bool enable_sampler_feedback = true;
    int m_sampledMipLevel = 0;
    int m_textureAreaIndex = 0; // sampled texture area index
    Vertex m_triangleVertices[90];
    XMFLOAT2 uv[4];// [2] ;

    UINT NumSubresources;// = desc.MipLevels;
    std::vector<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> Layouts;// (NumSubresources);
    std::vector<UINT> NumRows;// (NumSubresources);
    std::vector<UINT64> RowSizeInBytes;// (NumSubresources);
    //std::vector<UINT64> TotalBytes(NumSubresources);
    UINT64 TotalBytes;

    void LoadPipeline();
    void LoadAssets();
    std::vector<UINT8> GenerateTextureData();
    void PopulateCommandList();
    void WaitForPreviousFrame();
    void CreateMipMaps(ID3D12Device* _device, ID3D12GraphicsCommandList* commandList, ID3D12Resource* pTextureResource);
};

extern ComPtr<ID3D12DebugDevice> DebugDevice;
